define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	require("common");
	var abpUse = require("../abp.js");
	var abp = abpUse.abpstart;
	var template = require("template");

	var muiTips = require("muiTips"); //封装的弹框
	var keyWordText = ""; //关键字搜索
	//首页客户列表返回
	var commonBack = require("commonBack");

	//钉钉返回
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	mui("body").on("tap", "header>button", function() {
		document.location.href = "../../src/index.html?dd_nav_bgcolor=FFFF943E";
	});

	function getRightFun() {
		document.location.href = "../../src/newCheckStock/checkBegin.html?dd_nav_bgcolor=FFFF943E";
	}
	//ios
	dingTalkNav.LeftNavBar(commonBack.secondBack);
	//ad
	dingTalkNav.leftAndroid(commonBack.secondBack);
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityIdList = authorityInit.getJurisdictionId(authorityData); //权限一级判断
	var authorityWhCheck = authorityInit.getJurisdiction(authorityData, sourceType.WH_CHECK); //采购单权限判断

	if(authorityIdList.indexOf(sourceType.WH_CHECK) === -1) { //一级权限判断
		$('.mui-content').addClass('initContentTips');
		$('.mui-content > .tipsConInit').html("您没有权限操作盘点记录哦!");
		$("ul.pop-tab > li").addClass('mui-disabled');
		$("header > a").remove();

		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "新增", "default");

	} else {
		if(authorityWhCheck.indexOf(2) === -1) { //判断是否有添加复制权限
			$("header > a").remove();
			//钉钉右边按钮操作
			dingTalkNav.RightNavBar(false, "新增", "default");
		} else {
			$("header > a").show();
			//钉钉右边按钮操作
			dingTalkNav.RightNavBar(true, "新增", getRightFun);
		}
		if(authorityWhCheck.indexOf(1) === -1) { //判断是否有查询权限
			$('.mui-content > .tipsConInit').html("您没有权限查看盘点记录哦!");
			$('.mui-content').addClass("initContentTips");
			$("ul.pop-tab > li").addClass('mui-disabled');
		} else {

			var getFunList = require("../getFunList/getFunList.js"); //函数配置js
			getFunList.SortList("catalogList", "#catalog-items", 0, 0); //获取货品类别列表
			//调用日期筛选传值
			require("getSearchDateItem");
			//调用状态筛选值
			//	require("recordStatusID");
			//引入关闭tabjs
			var chioceTab = require("chioceTab");

			mui.init({
				swipeBack: true, //启用右滑关闭功能
				pullRefresh: { //配置分页下拉刷新
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});

			var start = "";
			var end = "";
			var StatusId = [];
			var CatalogId = [];
			var pageIndex = 1;
			var pageSize = 10;
			var dataState = true;
			var pageState = 10;
			var optionSource = {};
			var templateId = "checkList";
			var mainId = "#check-items";
			InitCheckList("initContentTips", true);

			//按时间筛选数据
			mui('body').on('tap', '.pop-tab-con1 li', function() {
				dataState = true;
				start = $(this).attr('data-stateDate');
				end = $(this).attr('data-endDate');
				InitCheckList("searchContentTips", false);
			});
			mui('body').on('tap', '#status-items li', function() {
				dataState = true;
				StatusId = [];
				var id = $(this).find("a").attr('data-id');
				if(id > 0) {
					if(id == 8) { //状态码8和9都统一为生效
						StatusId.push(9);
					}
					StatusId.push(id);
				}
				InitCheckList("searchContentTips", false);
			});
			mui('body').on('tap', '#catalog-items li', function() {
				dataState = true;
				CatalogId = [];
				var id = $(this).find("a").attr('data-id');
				if(id > 0) {
					CatalogId.push(id);
				}
				InitCheckList("searchContentTips", false);
			});
			mui('body').on('tap', '#check-items li', function() {
				sessionStorage.setItem('check_id', $(this).attr('id'));
			});

			//引入tab关闭
			mui("body").on("tap", ".pop-tab-conitem li", function() {
				chioceTab.muiSure();
			});

		}
	}

	function InitCheckList(contentTips, isChioce) {
		var BusUserId = [];
		var Serial = '';
		//  	var Serial='';

		var option = {};
		var dataModel = {
			"Serial": Serial, //单号
			"DeptId": 0, //供应商
			"BusUserId": BusUserId, //业务员
			"StatusId": StatusId, //状态
			"CatalogId": CatalogId, //货品分类
			"Start": start, //开始日期
			"End": end, //结束日期
			"OrderBy": "",
			"pageIndex": pageIndex,
			"pageSize": pageSize
		};
		abp.services.app.checkList.getPage(dataModel, option)
			.done(function(data) {
				var totalCountNum = data.totalCount; //获取数据列表条数
				pageState = Math.ceil(totalCountNum / pageSize);
				if(totalCountNum < 1) { //动态改变无数据提示
					$('.mui-content').addClass(contentTips);
					if(isChioce === true) {
						$('.mui-content > .tipsConInit').html("还没有库存盘点记录哦!");
					}
					$('.mui-pull-top-pocket').css('display', 'none');
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
				} else {
					$('.mui-content').removeClass(contentTips);
				}
				var items = data.items;
				$.each(items, function(i, v) {
					var serialDate = v.serialDate.substring(0, 10);
					v.serialDate = serialDate;
					var dataModified = v.dataModified.substring(0, 10);
					v.dataModified = dataModified;
					v.statusName = v.statusId == 1 ? "盘点中" : "盘点完成";
				});
				var html = template(templateId, data);
				if(dataState === true) { //判断是页面刷新还是上滑追加数据
					$(mainId).html(html);
				} else {
					$(mainId).append(html);
				}
				var statusIdColor = require("statusIdColor");
				statusIdColor($(".statusId"));
			})
			.fail(function(data) {

			});
	}

	function pulldownRefresh() {
		pageIndex = 1;
		dataState = true;
		pageState = 10;
		setTimeout(function() {
			InitCheckList("searchContentTips", false);
			mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //刷新完成
			//			mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);    //隐藏已经没数据了
			mui('#pullrefresh').pullRefresh().refresh(true); //重新启用页面上滑加载
		}, 1500);
	}

	function pullupRefresh() {
		pageIndex++;
		dataState = false;
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().endPullupToRefresh((pageState < pageIndex)); //参数为true代表没有更多数据了。
			InitCheckList("searchContentTips", false);
		}, 1500);
	}

});